﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using GetPriceService;
public partial class ProductDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(Request.QueryString["id"] == null)
        {
            Response.Redirect("Homepage.aspx");
        }
        else
        {
            LoadData();
        }
    }
    private void LoadData()
    {
        DataTable dtProduct = DataAccess.ExecuteQuery(String.Format("SELECT * FROM Products WHERE ProductID = '{0}'", Request.QueryString["id"]));
        if(dtProduct.Rows.Count < 1)
        {
            Response.Redirect("Homepage.aspx");
        }
        else
        {
            ABCBook getPrice = new ABCBook();
            Label lblABC = FormView1.FindControl("_lblABCBook") as Label;
            lblABC.Text = getPrice.GetProductPrice(Request.QueryString["ID"]);
            ASPxLabel _lblProductName = FormView1.FindControl("_lblProductName")as ASPxLabel;
            ASPxLabel _lblPrice = FormView1.FindControl("_lblPrice") as ASPxLabel;
            ASPxLabel _lblSaleOff = FormView1.FindControl("_lblSaleOff") as ASPxLabel;
            ASPxLabel _lblNewPrice = FormView1.FindControl("_lblNewPrice") as ASPxLabel;
            ASPxHyperLink _lnkAuthor = FormView1.FindControl("_lnkAuthor") as ASPxHyperLink;
            ASPxHyperLink _lnkPublisher = FormView1.FindControl("_lnkPublisher") as ASPxHyperLink;
            ASPxHyperLink _lnkCategory = FormView1.FindControl("_lnkCategory") as ASPxHyperLink;
            Image _img = FormView1.FindControl("_img") as Image;
            
            DataTable dtAuthor = DataAccess.ExecuteQuery(String.Format("SELECT AuthorID, AuthorName FROM Authors WHERE AuthorID = '{0}'", dtProduct.Rows[0]["AuthorID"]));
            DataTable dtCategory = DataAccess.ExecuteQuery(String.Format("SELECT CategoryID, CategoryName FROM Categories WHERE CategoryID = '{0}'", dtProduct.Rows[0]["CategoryID"]));
            DataTable dtPublisher = DataAccess.ExecuteQuery(String.Format("SELECT PublisherID, PublisherName FROM Publishers WHERE PublisherID = '{0}'", dtProduct.Rows[0]["PublisherID"]));

            _lblProductName.Text = dtProduct.Rows[0]["ProductName"].ToString();
            _lblPrice.Text = String.Format("{0:N0}đ", dtProduct.Rows[0]["Price"]);
            _lblSaleOff.Text = String.Format("{0:N0}đ", dtProduct.Rows[0]["SaleOff"]);
            _lblNewPrice.Text = String.Format("{0:N0}đ (Giảm {1:P2})", -float.Parse(dtProduct.Rows[0]["SaleOff"].ToString()) + float.Parse(dtProduct.Rows[0]["Price"].ToString()), float.Parse(dtProduct.Rows[0]["SaleOff"].ToString()) / float.Parse(dtProduct.Rows[0]["Price"].ToString()));
            _img.ImageUrl = dtProduct.Rows[0]["Image"].ToString();
            if(dtAuthor.Rows.Count > 0)
            {
                _lnkAuthor.Text = dtAuthor.Rows[0][1].ToString();
            }
            else
            {
                _lnkAuthor.Text = "";
            }
            if (dtPublisher.Rows.Count > 0)
            {
                _lnkPublisher.Text = dtPublisher.Rows[0][1].ToString();
            }
            else
            {
                _lnkPublisher.Text = "";
            }
            if (dtCategory.Rows.Count > 0)
            {
                _lnkCategory.Text = dtCategory.Rows[0][1].ToString();
            }
            else
            {
                _lnkCategory.Text = "";
            }
            Label _lblExist = FormView1.FindControl("_lblExist") as Label;
            RangeValidator _RangeValidatorAmount = FormView1.FindControl("_RangeValidatorAmount") as RangeValidator;
            string existAmount = dtProduct.Rows[0]["Amount"].ToString();
            _lblExist.Text = "/" + existAmount;
            _RangeValidatorAmount.MaximumValue = existAmount;
        }
    }
    
    protected void _lnkAddToCart_Click(object sender, EventArgs e)
    {
        TextBox _txtAmount = FormView1.FindControl("_txtAmount") as TextBox;
        ImageButton _imgAddToCart = FormView1.FindControl("_imgAddToCart") as ImageButton;
        MyShoppingCart shoppingCart = Session["ShoppingCart"] as MyShoppingCart;
        if(shoppingCart != null)
        {
            shoppingCart.AddItem(_imgAddToCart.CommandArgument, int.Parse(_txtAmount.Text));
            Response.Redirect("ProductDetail.aspx?id=" + _imgAddToCart.CommandArgument);
        }
        else
        {
            Response.Write("<script> alert('Chưa có giỏ hàng') </script>");
        }
    }
}
